/*******************************************************************************
GTileMap.cpp

	Author:		LiuDingXi
	e-mail:		liudx@gmail.com
	Date time:	2009-08-07

	Copyright (C) 2009
	All rights reserved.
	
*******************************************************************************/
#include "StdAfx.h"
#include "GTileMap.h"

#if (_OS_ == _OS_WINNT_)
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif//(_OS_ == _OS_WINNT_)
////////////////////////////////////////////////////////////////////////////////

GMap::GMap()
{
	m_pBase = NULL;
	m_pThis = NULL;
	m_pImages = NULL;
}

GMap::~GMap()
{
	delete[] m_pImages;
	m_pImages = NULL;
}

BOOL GMap::DataBind(GSight* pSight, const BYTE* pBase, SceneDat::PTR ptr)
{
	m_pBase = pBase;
	m_pThis = (const MAP*)(pBase + ptr);

	return TRUE;
}

BOOL GMap::Init(LPCTSTR szFolder, GDC* pDC)
{
	if(m_pThis)
	{
		if(m_pThis->nImg > 0)
		{
			TCHAR szImageFile[MAX_PATH];
			_tcscpy(szImageFile, szFolder);
			TCHAR* pFile = szImageFile;
			while(*pFile) pFile++;
			
			m_pImages = new GImage[m_pThis->nImg];

			const TIMG* pImgs = (const TIMG*)(m_pBase + m_pThis->pImg);

			for(int i=0; i<m_pThis->nImg; i++)
			{
				_tcscpyA2T(pFile, pImgs->szImg);
				m_pImages[i].Load(szImageFile);
				pImgs++;
			}
		}

		return TRUE;
	}
	return FALSE;
}

BOOL GMap::IsValid()const
{
	if(m_pThis)
	{
		if(m_pThis->nImg > 0)
		{
			if(m_pImages == NULL)
				return FALSE;
		}
		return TRUE;
	}
	return FALSE;
}

void GMap::Update(UINT nMillisecond)
{
}

bool GMap::IsVisible()const
{
	if(m_pThis)
	{
		return true;
	}
	return false;
}

void GMap::OnDraw(GDC* pDC)
{
	if(m_pThis && m_pImages)
	{
		const TIMG* pImgs = (const TIMG*)(m_pBase + m_pThis->pImg);
		const TILE* pTile = (const TILE*)(m_pBase + m_pThis->pTile);

		for(int i=0; i<m_pThis->nTile; i++)
		{
			int nIndex = pTile[i].idx;
			const TIMG* pImg = pImgs + nIndex;

			GCLIP Clip;
			Clip.x = pImg->x;
			Clip.y = pImg->y;
			Clip.cx = pImg->cx;
			Clip.cy = pImg->cy;

			GOPER Oper;
			Oper.fAlpha = 0;
			Oper.fAngle = 0;

			pDC->Draw(m_pImages[nIndex], pTile[i].x, pTile[i].y, Clip, Oper);
		}
	}
}

SINT GMap::HitTest(int x, int y)const
{
	if(m_pThis)
	{
		const POS* pPos = (const POS*)(m_pBase + m_pThis->pPos);
		for(int i=0; i<m_pThis->nPos; i++)
		{
			const POS& refPos = pPos[i];
			if( x > refPos.x &&
				y > refPos.y &&
				x < refPos.x + refPos.cx &&
				y < refPos.y + refPos.cy )
			{
				return i;
			}
		}
	}
	return -1;	
}

BOOL GMap::GetPos(int i, POS& refPos)const
{
	if(m_pThis)
	{
		const POS* pPos = (const POS*)(m_pBase + m_pThis->pPos);
		if(i>=0 && i<m_pThis->nPos)
		{
			refPos = pPos[i];
			return TRUE;
		}
		
	}
	return FALSE;
}

BOOL GMap::GetPos(POS& refPos)const
{
	return FALSE;
}
BOOL GMap::GetBox(BOX& refBox)const
{
	return FALSE;
}
BOOL GMap::GetCxy(CXY& refCxy)const
{
	return FALSE;
}


GTileMap::GTileMap()
{
}

GTileMap::~GTileMap()
{
}

